package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.DBManager;

public class CustomAlertDialog extends Dialog implements View.OnClickListener {
    Button cancelBtn,sureBtn;
    int consumptionId;

    public void getConsumptionId(int id){
        consumptionId = id;
    }
    public interface OnSureListener{
        public void onSure();
    }

    OnSureListener onSureListener;

    public void setOnSureListener(OnSureListener onSureListener) {
        this.onSureListener = onSureListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom_alert);
        initView();
    }

    private void initView() {
        cancelBtn = findViewById(R.id.dialog_alert_cancel);
        sureBtn = findViewById(R.id.dialog_alert_sure);

        cancelBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    public CustomAlertDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_alert_cancel:
                cancel();
                break;
            case R.id.dialog_alert_sure:
                DBManager.deleteConsumption(consumptionId);
                onSureListener.onSure();
                cancel();
                break;
        }
    }
    //    设置Dialog控件显示位置
    public void setClassDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }
}
